﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(LanguageText), true), CanEditMultipleObjects]
public class LanguageTextEditor : UnityEditor.Editor
{
    LanguageText _languageText;
    public override void OnInspectorGUI()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PropertyField(serializedObject.FindProperty("WithID"));
        if (GUILayout.Button("Check")) 
        {
            CheckText();
        }
        EditorGUILayout.EndHorizontal();
        if (_languageText.Text == null)
        {
            EditorGUILayout.HelpBox("Text component is NULL!", MessageType.Error);
        }
        if (_languageText.WithID)
            EditorGUILayout.PropertyField(serializedObject.FindProperty("ID"));
        else 
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Key"));
        if (GUILayout.Button("UpdateLanguage"))
        {
            UpdateLanguage();
        }
        this.serializedObject.ApplyModifiedProperties();
    }

    private void OnEnable()
    {
        if (_languageText == null)
            this._languageText = (LanguageText)this.serializedObject.targetObject;
        CheckText();
    }

    private void CheckText() 
    {
        if (_languageText.Text == null)
        {
            _languageText.Text = _languageText.GetComponent<Text>();
        }
    }

    private void UpdateLanguage() 
    {
        _languageText.RefreshLanguage();
        EditorUtility.SetDirty(_languageText.Text);
    }
}
